package com.smsp.controller;


import com.github.pagehelper.PageInfo;
import com.smsp.common.Const;
import com.smsp.common.ServerResponse;
import com.smsp.entity.Templet;
import com.smsp.entity.User;
import com.smsp.service.TempletService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("template/")
public class TempletController {

    @Autowired
    private TempletService templetService;

    public Logger logger = LoggerFactory.getLogger(TempletController.class);

    /**
     * 模板列表视图
     *
     * @return
     */
    @RequestMapping(value = "templatePage.do")
    public String templatePage() {
        return "template";
    }

    /**
     * 添加模板视图
     *
     * @return
     */
    @RequestMapping(value = "addTemplatePage.do")
    public String addTemplatePage() {
        return "addTemplate";
    }

    /**
     * 我的模板视图
     *
     * @return
     */
    @RequestMapping(value = "myTemplatePage.do")
    public String myTemplatePage() {
        return "myTemplate";
    }


    @RequestMapping(value = "create.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse create(@Validated @RequestBody Templet templet, BindingResult result, HttpSession session) {

        if (result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            logger.debug(allErrors.get(0).getDefaultMessage(), allErrors);
            return ServerResponse.createByErrorMessage(allErrors.get(0).getDefaultMessage());
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(Const.ERROR_USER_NEED_LOGIN, "用户需要登录");
        }
        ServerResponse serverResponse = templetService.insertTemplet(templet, user);
        return serverResponse;
    }


    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse delete(Templet templet) {
        ServerResponse serverResponse = templetService.deleteTemplet(templet);
        return serverResponse;
    }

    @RequestMapping(value = "update.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse update(Templet templet) {
        ServerResponse serverResponse = templetService.updateTemplet(templet);
        return serverResponse;
    }


    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse list() {
        ServerResponse serverResponse = templetService.selectAllTemplet();
        return serverResponse;
    }

    @RequestMapping(value = "passStatus.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse passStatus(@RequestBody Templet templet) {
        templet.setTempletStatus("1");
        templet.setUpdateTime(new Date());
        return templetService.updateTempletStatus(templet);
    }

    @RequestMapping(value = "stopStatus.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse stopStatus(@RequestBody Templet templet) {
        templet.setTempletStatus("2");
        templet.setUpdateTime(new Date());
        return templetService.updateTempletStatus(templet);
    }

    @RequestMapping(value = "selectListByUserId.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse selectListByUserId(HttpSession session) {
        User user = (User) session.getAttribute("user");
        return templetService.selectListByUserId(user.getId());
    }

    @RequestMapping(value = "selectPassTempletByUserId.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse selectPassTempletByUserId(Integer userId) {
        return templetService.selectPassTempletByUserId(userId);
    }


    @RequestMapping(value = "listPage.do", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllByPage(int page, int pageSize) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        PageInfo<Templet> pager = templetService.findMessageDetailByPage(page, pageSize);
        //总条数
        resultMap.put("total", pager.getTotal());
        //获取每页数据
        resultMap.put("rows", pager.getList());
        return resultMap;

    }


}

